package com.riku.security.controller;

import com.riku.security.common.SeCommonResponse;
import com.riku.security.core.SeAuthorityService;
import com.riku.security.core.SeExternalUserService;
import com.riku.security.model.SeSecurityApi;
import com.riku.security.model.SeSecurityMenu;
import com.riku.security.model.SeService;
import com.riku.security.model.SeUserParam;
import com.riku.security.service.SeCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by 1 on 2021/5/23.
 */
@Api(value = "security-common",description = "权限-公共")
@RestController
@RequestMapping("/se/common/")
@ConditionalOnBean(SeAuthorityService.class)
public class SeCommonController {

    @Autowired
    private SeCommonService seCommonService;

    @Autowired
    private SeExternalUserService seExternalUserService;


    @ApiOperation("服务权限-当前人员")
    @GetMapping("selectService")
    public SeCommonResponse<List<SeService>> selectService(
            @ApiParam(required = false) @RequestParam(required = false) String serviceName
    ) {
        return SeCommonResponse.success(seCommonService.selectService(serviceName,seExternalUserService.getNowUser().getUserId()));
    }

    @ApiOperation("菜单权限-当前人员")
    @GetMapping("selectSecurityMenu")
    public SeCommonResponse<List<SeSecurityMenu>> selectSecurityMenu(
            @ApiParam(required = true,value = "多个用,隔开") @RequestParam(required = true) String serviceCode,
            @ApiParam(required = false,allowableValues="0,1") @RequestParam(required = false) Integer includeApi,
            @ApiParam(required = false,allowableValues="0,1") @RequestParam(required = false) Integer tree
    ) {
        return SeCommonResponse.success(seCommonService.selectMenuByUserId(serviceCode,seExternalUserService.getNowUser().getUserId(),includeApi,tree));
    }
    @ApiOperation("接口权限-当前人员")
    @GetMapping("selectSecurityApi")
    public SeCommonResponse<List<SeSecurityApi>> selectSecurityApi(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode
    ) {
        return SeCommonResponse.success(seCommonService.selectApiByUserId(serviceCode,seExternalUserService.getNowUser().getUserId()));
    }

    @ApiOperation("查询人员")
    @GetMapping("searchUser")
    public SeCommonResponse<List<SeUserParam>> searchUser(
            @ApiParam(required = true) @RequestParam(required = true) String userContent
    ) {
        return SeCommonResponse.success(seExternalUserService.searchUser(userContent));
    }
}
